#include <iostream>
#include <winsock2.h>

#pragma comment(lib, "WS2_32.lib")

const unsigned int PORT = 11024;
const int kBufferSize = 1024;

int main() {
	WSADATA wsData;
	if (WSAStartup(MAKEWORD(2, 2), &wsData) != 0) {
		std::cout << "start failed" << std::endl;
		return -1;
	}
	//
	SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	SOCKADDR_IN addIn;

	addIn.sin_family = AF_INET;
	addIn.sin_port = htons(PORT);
	addIn.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
	int bBindRet = bind(s, (LPSOCKADDR)&addIn, sizeof(addIn));
	if (bBindRet != 0) {
		std::cout << "bind failed" << std::endl;
		return -1;
	}

	char cRecvBuffer[kBufferSize];
	int ilen = sizeof(addIn);
	int iRecv = recvfrom(s, cRecvBuffer, kBufferSize, 0, (LPSOCKADDR)&addIn, &ilen);
	while (iRecv != SOCKET_ERROR) {
		std::cout << "recvie information: " << std::endl;
		char cSendBuffer[kBufferSize];
		std::cout << "Input the information that the client sends; " << std::endl;
		std::cin >> cSendBuffer;
		sendto(s, cSendBuffer, kBufferSize, 0, (LPSOCKADDR)&addIn, sizeof(addIn));
	}

	if (WSACleanup() != 0) {
		std::cout << "Clean up failed" << std::endl;
		return -1;
	}

	return 0;
}